package tree.tree2.symmetry.maxAndMinTree;

import tree.TreeNode;

/**
 * @ClassName IsBalanced
 * @Description TODO
 * @Author lenovo
 * @Date 2023-05-04 9:41
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class IsBalanced {

    public boolean isBalanced(TreeNode root) {

        return judgeIsBalaced(root) == -1 ? false : true;
    }

    public int judgeIsBalaced(TreeNode root) {
        if (root == null)
            return 0;

        int left = judgeIsBalaced(root.left);
        int right = judgeIsBalaced(root.right);
        if (left == -1 || right == -1 || Math.abs(left - right) >1) {
            return -1;
        }
        return Math.max(left, right) + 1;
    }

    public static void main(String[] args) {
        IsBalanced isBalanced = new IsBalanced();

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        System.out.println(isBalanced.judgeIsBalaced(root));
    }
}